package com.ysd.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ysd.entity.Consume_log;
import com.ysd.repository.Consume_logRepository;
import com.ysd.utils.Result;

@RestController
public class Consume_logController {
	@Autowired
	private Consume_logRepository cR;
	@Resource
	private SimpleDateFormat dateFormat;
	@RequestMapping("addConsume_log")
	public Object addConsume_log(Consume_log consume,int card_id,int read_room) {
		long time = 30*60*1000;//30分钟
		String in_time=dateFormat.format(new Date());
		
		int n=cR.addConsume(card_id, in_time,read_room);
		if (n>0) {
			return Result.toClient("0", "进入成功", null);
		} else {
			return Result.toClient("1", "进入失败", null);
		}
		
	}
	@RequestMapping("selAll")
	public Object selAll() {
		List<Consume_log> list=cR.selectAll();
		long n=cR.count();
		int count=(int) n;
		
		if (list!=null) {
			return Result.toClient("0", "查询成功", count, list);
		} else {
			return Result.toClient("1", "查询失败", null, null);
		}
	}
	@RequestMapping("updStatus")
	public Object updStatus(int mId,int rId) {
		int n=cR.updateStatus(mId, rId);
		if (n>0) {
			return Result.toClient("0", "修改成功", null);
		} else {
			return Result.toClient("1", "修改失败", null);
		}
	}
	@RequestMapping("upConsome_logOutTime")
	public	Object upConsome_logOutTime(int cId,Consume_log consume) {
		long time = 30*60*1000;//30分钟
		
		String outTime=dateFormat.format(new Date().getTime()+time);
		consume.setOut_time(outTime);
		int n=cR.xiuGaiStatus(outTime,cId);
		if (n>0) {
			return Result.toClient("0", "离开成功", null);
		} else {
			return Result.toClient("1", "离开失败", null);
		}
	}
}
